package com.telnetnz.ticketdesk.action;

import java.util.ArrayList;
import java.util.List;

import com.telnetnz.ticketdesk.action.base.BasicSessionPageAccAction;
import com.telnetnz.ticketdesk.action.common.CommonUtils;
import com.telnetnz.ticketdesk.bean.ProjectToUser;
import com.telnetnz.ticketdesk.bean.Ticket;
import com.telnetnz.ticketdesk.service.ProjectService;
import com.telnetnz.ticketdesk.service.ProjectToUserService;
import com.telnetnz.ticketdesk.service.TicketService;

public class IndexAction extends BasicSessionPageAccAction
{
	public String doLanding()
	{
		long userId = getSession().getSession_uuid();

		List<ProjectToUser> projectToUsers = ProjectToUserService.getByUserId(userId);
		List<Integer> projectIds = new ArrayList<Integer>(); // The ids for building the drop down list
		for (ProjectToUser projectToUser : projectToUsers)
		{
			projectIds.add(projectToUser.getProjectId());
		}

		List<Long> ownerIds = new ArrayList<Long>();
		ownerIds.add(userId);

		List<Integer> statuses = new ArrayList<Integer>();
		statuses.add(TicketService.STATUS_CLOSED);
		statuses.add(TicketService.STATUS_CANCELED);

		List<Ticket> myTickets = TicketService.getByPageAndCondition(
				0, 
				15, 
				TicketService.ORDERBY_PRIORITY, 
				0, 
				projectIds, 
				0L, 
				0L, 
				ownerIds, 
				0L, 
				0L, 
				null, 
				null, 
				-1, 
				-1, 
				null, 
				statuses, 
				0L, 
				null, 
				null, 
				null, 
				null, 
				null, 
				null);

		List<Ticket> myClosedTickets = TicketService.getByPageAndCondition(
				0, 
				15, 
				TicketService.ORDERBY_UPDATETIME, 
				0, 
				projectIds, 
				0L, 
				0L, 
				ownerIds, 
				0L, 
				0L, 
				null, 
				null, 
				-1, 
				-1, 
				statuses, 
				null, 
				0L, 
				null, 
				null, 
				null, 
				null, 
				null, 
				null);
		
		
		ownerIds = new ArrayList<Long>();
		ownerIds.add(0L);

		List<Ticket> unassignedTickets = TicketService.getByPageAndCondition(
				0, 
				15, 
				TicketService.ORDERBY_CREATETIME, 
				0, 
				projectIds, 
				0L, 
				0L, 
				ownerIds, 
				0L, 
				0L, 
				null, 
				null, 
				-1, 
				-1, 
				null, 
				null, 
				0L, 
				null, 
				null, 
				null, 
				null, 
				null, 
				null);

		put_page_key("project_options", CommonUtils.buildProjectDropdown(
				ProjectService.getByPageAndCondition(0, 100, ProjectService.ORDERBY_NAME, 1, null), 
				0, 
				false));
		put_page_key("post_action", baseLink("ticketadd"));
		put_page_key("my_tickets", myTickets);
		put_page_key("latest_tickets", unassignedTickets);
		put_page_key("closed_tickets", myClosedTickets);
		return SUCCESS;
	}
}
